
package com.zzyl.web.controller.member;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.R;

import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.thread.UserTheadLocal;
import com.zzyl.nursing.domain.pojo.Reservation;
import com.zzyl.nursing.service.ReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 预约信息Controller
 *
 * @author ruoyi
 */

@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class MemberReservationController extends BaseController {
    @Autowired
    private ReservationService reservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        // 获取当前登录用户ID
        Long userId = UserTheadLocal.getUser();
        // 获取当天开始和结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        // 调用服务层查询取消预约次数
        Integer count = reservationService.getCancelledCount(userId, startTime, endTime);
        return R.ok(1);
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public R<Integer> getReservationCountByTime(String startTime, String endTime) {
        return R.ok(reservationService.getReservationCountByTime(startTime, endTime));
    }

    @PostMapping
    @ApiOperation("新增预约")
    public R<Integer> postReservation(@RequestBody Reservation reservation) {
        // 调用服务层新增预约
        boolean result = reservationService.saves(reservation);
        return R.ok();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询预约信息")
    public R<TableDataInfo> page(Reservation reservation) {
        Long userId = UserTheadLocal.getUser();
        startPage();
        // 添加用户过滤条件
        LambdaQueryWrapper<Reservation> wrapper = Wrappers.<Reservation>lambdaQuery(reservation)
                .eq(Reservation::getCreateBy, userId)
                .orderByDesc(Reservation::getCreateTime);
        List<Reservation> list = reservationService.list(wrapper);
        TableDataInfo tableDataInfo = getDataTable(list);
        return R.ok(tableDataInfo);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("修改预约信息")
    public R<Integer> cancelReservation(@PathVariable Long id) {
        // 调用服务层修改预约信息
        Reservation reservation = new Reservation();
        reservation.setId(id);
        reservation.setStatus(2);
        boolean result = reservationService.updateById(reservation);
        return R.ok();
    }
}
